package com.sz.app.oss.api.common.base;


import com.sz.app.oss.constants.MessagesCodes;
import com.sz.app.oss.constants.Permissions;
import com.sz.biz.app.web.BaseController;
import com.sz.biz.common.base.entity.FeeType;
import com.sz.biz.common.base.service.FeeTypeService;
import com.sz.common.base.dto.I18nDto;
import com.sz.common.base.dto.QueryParams;
import com.sz.common.base.dto.ResultDto;
import com.sz.common.base.service.I18nBaseService;
import com.sz.common.base.utils.EntityUtils;
import com.sz.common.base.utils.StringUtils;
import com.sz.common.core.system.entity.SysDict;
import com.sz.common.core.system.service.SysDictService;
import com.sz.app.oss.api.common.base.dto.FeeTypeDto;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * Function: 费用类型 controller <br>
 * Author: hpeng.wang <br>
 * Date: 2016-12-07 11:08:00
 */

@RestController
@RequestMapping("${api}/biz/feetypes")
@Api(description = " ", tags = "022、费用类型管理")
public class FeeTypeController extends BaseController {
    @Autowired
    private FeeTypeService service;
    @Autowired
    private SysDictService dictService;
    @Autowired
    private I18nBaseService i18nService;
    @Value("${i18n.biz.fee.type}")
    private String i18nTable;

    /**
     * 新增.
     */
    @ApiOperation(value = "新增", notes = "新增费用类型数据<br>权限：" + Permissions.BIZ_MGT_BASE_FEETYPE)
    @RequestMapping(value = "", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_FEETYPE)
    public ResultDto save(@RequestBody @ApiParam(value = "要保存的数据", required = true) @Validated FeeTypeDto dto) {
        FeeType entry = new FeeType();
        dto.setId(null);//保存时忽略FeeId
        EntityUtils.copyPropertiesIgnoreNull(dto, entry);
        entry.setLastUpdateTime(new Date());
        int id = service.save(entry);
        i18nService.save(id, dto.getI18n(), i18nTable);
        ResultDto result = getSaveSuccessDto(MessagesCodes.MODULE_FEETYPE);
        result.setData(id);
        return result;

    }

    /**
     * 批量删除
     *
     * @param ids 要删除的id集合
     * @return 操作结果
     */
    @ApiOperation(value = "删除", notes = "根据指定id集合删除数据<br>权限：" + Permissions.BIZ_MGT_BASE_FEETYPE, httpMethod = "POST")
    @RequestMapping(value = "/deletions", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_FEETYPE)
    public ResultDto batchDelete(@RequestBody @ApiParam(value = "要删除的id集合", required = true) List<Integer> ids) {
        service.batchdelete(ids);
        ResultDto dto = getDeleteSuccessDto(MessagesCodes.MODULE_FEETYPE);
        return dto;
    }

    /**
     * 修改.
     */
    @RequestMapping(value = "{id}", method = {RequestMethod.POST})
    @ApiOperation(value = "修改", notes = "修改费用类型数据<br>权限：" + Permissions.BIZ_MGT_BASE_FEETYPE)
    @Transactional(propagation = Propagation.REQUIRED)
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_FEETYPE)
    public ResultDto update(@PathVariable int id, @RequestBody FeeTypeDto dto) {
        int count = 0;
        FeeType entry = service.findById(id);
        if (entry != null) {
            dto.setId(null);
            EntityUtils.copyPropertiesIgnoreNull(dto, entry);
            entry.setLastUpdateTime(new Date());
            count = service.update(entry);
            i18nService.save(id, dto.getI18n(), i18nTable);
        }
        ResultDto result = getUpdateSuccessDto(MessagesCodes.MODULE_FEETYPE);
        result.setData(count);
        return result;
    }

    /**
     * 根据id查询数据
     *
     * @param id 费用类型的id
     * @return 查询到的单条费用类型
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "查询_Id", notes = "根据id获取到的费用类型")
    public ResultDto findById(@ApiParam(value = "费用类型id", required = true) @PathVariable Integer id) {
        FeeTypeDto dto = new FeeTypeDto();
        FeeType entry = service.findById(id);
        if (entry != null) {
            EntityUtils.copyPropertiesIgnoreNull(entry, dto);
            List<I18nDto> list = i18nService.findI18nDtoByRefId(i18nTable, entry.getId());
            dto.setI18n(list);
        }
        ResultDto result = new ResultDto();
        result.setData(dto);
        return result;
    }

    /**
     * 根据条件获取数据集合
     *
     * @param q         参数信息
     * @param pageIndex 哪页
     * @param pageSize  分页大小
     * @param sort      排序字段的名称
     * @param asc       true asc false desc
     * @return 查询的数据集合
     */
    @ApiOperation(value = "查询", notes = "根据指定条件获取数据集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "q", value = "模糊查询参数", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "分页", dataType = "int", paramType = "query", defaultValue
                    = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数,max<2000", dataType = "int", paramType = "query",
                    defaultValue = "10"),
            @ApiImplicitParam(name = "sort", value = "排序的字段", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "类型", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "asc", value = "排序方式", dataType = "boolean", paramType = "query", defaultValue =
                    "true")
    })

    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResultDto find(@RequestParam(required = false) String q,
                          @RequestParam(required = false, defaultValue = "1") int pageIndex,
                          @RequestParam(required = false, defaultValue = "10") int pageSize,
                          @RequestParam(required = false) String sort,
                          @RequestParam(required = false) String type,
                          @RequestParam(required = false, defaultValue = "true") Boolean asc
    ) {
        q = StringUtils.replaceSqlPattern(q);
        ResultDto result = service.findByParam(new QueryParams(pageSize, pageIndex, q, sort, asc).put("type", type));
        return result;
    }

    @ApiOperation(value = "查询费用类型的类型", notes = "查询费用类型的类型")
    @RequestMapping(value = "/type", method = RequestMethod.GET)
    public ResultDto findType() {
        List<SysDict> dictList = dictService.findByCatalog("biz.feetype.type");
        ResultDto dto = new ResultDto();
        dto.setData(dictList);
        return dto;

    }

    /**
     * 批量新增或修改国际化.
     *
     * @param dtos 要保存或修改的国际化实体集合
     * @return 处理成功与否的返回信息
     */
    @ApiOperation(value = "新增或修改i18n", notes = "批量新增或修改费用类型国际化<br>权限：" + Permissions.BIZ_MGT_BASE_FEETYPE)
    @RequestMapping(value = "i18n", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_FEETYPE)
    public ResultDto saveI18n(@RequestBody @ApiParam(value = "要保存的对象集合", required = true) List<I18nDto> dtos) {
        i18nService.save(dtos, i18nTable);
        service.refreshCache();
        ResultDto result = this.getSaveI18nSuccessDto();
        return result;
    }


    /**
     * 根据条件获取国际化数据集合
     *
     * @param language  语言信息
     * @param q         参数信息
     * @param pageIndex 哪页
     * @param pageSize  分页大小
     * @param unfilled  是否未添加国际化字段，true，代表未添加，fasle与null代表所有
     * @param sort      排序字段的名称
     * @param asc       true asc false desc
     * @return 查询的国际化数据集合
     */
    @ApiOperation(value = "查询i18n", notes = "根据指定条件获取国际化数据集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "language", value = "语言信息", dataType = "string", paramType = "path", required =
                    true),
            @ApiImplicitParam(name = "q", value = "模糊查询参数", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "分页", dataType = "int", paramType = "query", defaultValue =
                    "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数,max<2000", dataType = "int", paramType = "query",
                    defaultValue = "10"),
            @ApiImplicitParam(name = "unfilled", value = "是否未添加国际化字段，true，代表未添加，fasle与null代表所有", dataType = "boolean", paramType = "query", defaultValue =
                    "false"),
            @ApiImplicitParam(name = "sort", value = "排序的字段", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "asc", value = "排序方式", dataType = "boolean", paramType = "query", defaultValue =
                    "true")
    }

    )
    @RequestMapping(value = "i18n/{language}", method = RequestMethod.GET)
    public ResultDto findI18n(@PathVariable String language,
                              @RequestParam(required = false) String q,
                              @RequestParam(required = false, defaultValue = "1") int pageIndex,
                              @RequestParam(required = false, defaultValue = "10") int pageSize,
                              @RequestParam(required = false, defaultValue = "false") Boolean unfilled,
                              @RequestParam(required = false) String sort,
                              @RequestParam(required = false, defaultValue = "true") Boolean asc) {
        q = StringUtils.replaceSqlPattern(q);
        ResultDto result = service.findI18nByParam(new QueryParams(pageSize, pageIndex, q, sort, asc, language).put("unfilled", unfilled));
        return result;
    }


    /**
     * 异步校验费用类型的code
     *
     * @param code 费用类型code
     * @param sid  　费用类型id，修改时传id，新增时为NULL
     * @return　校验的结果
     */
    @ApiOperation(value = "异步校验费用类型名编码", notes = "异步校验费用类型编码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "费用类型编码", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "sid", value = "费用类型id 修改时传id 新增时传空", dataType = "int", paramType = "query")})
    @RequestMapping(value = "/feetypecheckcode", method = {RequestMethod.GET})
    public ResultDto feeTypeCheckCode(@RequestParam String code,
                                      @RequestParam(required = false, defaultValue = "0") int sid) {
        service.feeTypeCheckCode(sid, code);
        return new ResultDto();
    }
}



